package com.demo.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.dao.FormSourceDao;
import com.demo.core.dao.FormSourceParamDao;
import com.demo.core.model.FormSource;
import com.demo.core.model.FormSourceParam;
import com.demo.core.service.base.FormSourceService;

/**
 * 自定义表单数据源服务的实现类
 * @author ryuu.kk
 *
 */
public class FormSourceServiceImpl implements FormSourceService {

	/**
	 * 表单资源数据库访问接口
	 */
	private FormSourceDao formSourceDao;
	
	/**
	 * 表单资源参数数据库访问接口
	 */
	private FormSourceParamDao formSourceParamDao;
	
	@Override
	public Long save(FormSource formSource) {
		return formSourceDao.insert(formSource);
	}

	@Override
	public Long save(FormSourceParam formSourceParam) {
		return formSourceParamDao.insert(formSourceParam);
	}

	@Override
	public void save(Long sourceId, List<FormSourceParam> formSourceParamList) {
		if (formSourceParamList != null) {
			for (FormSourceParam param : formSourceParamList) {
				param.setSourceId(sourceId);
				formSourceParamDao.insert(param);
			}
		}
	}

	@Override
	public void deleteSource(Long id) {
		formSourceDao.delete(id);
	}

	@Override
	public void deleteSourceParam(Long id) {
		formSourceParamDao.delete(id);
	}

	@Override
	public void deleteSourceParams(Long sourceId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sourceId", sourceId);
		formSourceParamDao.delete(params);
	}

	@Override
	public void updateSource(Long id, String title, String ds, String source) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		FormSource formSource = new FormSource();
		formSource.setDs(ds);
		formSource.setSource(source);
		formSource.setTitle(title);
		formSourceDao.update(formSource, params);
	}

	@Override
	public void updateSource(Long id, String type) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		FormSource formSource = new FormSource();
		formSource.setType(type);
		formSourceDao.update(formSource, params);
	}

	@Override
	public void updateSourceParam(Long id, String title, String defaultValue) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		FormSourceParam formSourceParam = new FormSourceParam();
		formSourceParam.setTitle(title);
		formSourceParam.setDefaultValue(defaultValue);
		formSourceParamDao.update(formSourceParam, params);
	}

	@Override
	public void updateSourceParam(Long id, String type) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		FormSourceParam formSourceParam = new FormSourceParam();
		formSourceParam.setType(type);
		formSourceParamDao.update(formSourceParam, params);
	}

	@Override
	public FormSource findSource(Long id) {
		return formSourceDao.select(id);
	}

	@Override
	public List<FormSource> findSourceList(Long formId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("formId", formId);
		return formSourceDao.select(params);
	}

	@Override
	public FormSourceParam findSourceParam(Long id) {
		return formSourceParamDao.select(id);
	}

	@Override
	public List<FormSourceParam> findSourceParamList(Long sourceId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sourceId", sourceId);
		return formSourceParamDao.select(params);
	}

	@Override
	public List<FormSource> querySourceList(Long formId) {
		List<FormSource> sourceList = findSourceList(formId);
		
		if (sourceList != null) {
			for (FormSource source : sourceList) {
				List<FormSourceParam> formSourceParamList = findSourceParamList(source.getId());
				source.setFormSourceParamList(formSourceParamList);
			}
		}
		return sourceList;
	}
	
	public void setFormSourceDao(FormSourceDao formSourceDao) {
		this.formSourceDao = formSourceDao;
	}

	public void setFormSourceParamDao(FormSourceParamDao formSourceParamDao) {
		this.formSourceParamDao = formSourceParamDao;
	}
}
